#include "util.h"
#include <stdint.h>

/**
 * @brief Convert float to hex by the IEEE 754 standard
 *
 * @param f float value to convert
 * @param hex pointer to store the hex value of float (32bit), uint8_t[4]
 * @return Status OK or ERROR
 */
Status FloatToHex(float f, uint8_t* hex)
{
    // 1. use union to operate the float
    typedef union
    {
        uint32_t i;
        float f;
    } u;

    // 2. set a.f to f
    u a = {
        .f = f
    };

    // 3. use mask to get hex value of float
    uint32_t mask = 0x000000FF;
    for (int i = 0; i < 4; i++)
    {

        hex[i] = (a.i & mask); // float in big-endian：eg 0x12345678，hex[0] = 0x78
        // hex[3 - i] = (a.i & mask); // float in little-endian: eg 0x12345678，hex[0] = 0x12
        a.i = a.i >> 8;
    }
    return UTIL_OK;
}

/* -----------------------------------------------------------------------
 * 以下函数依赖于 "main.h" 
 -------------------------------------------------------------------------*/
#include "main.h"

void soft_reset(void)
{
	// close all interrupts
    __set_FAULTMASK(1); 
    // software reset
    NVIC_SystemReset(); 
}